//
//  Tumble.m
//  PezMonkey
//
//  Created by Spence DiNicolantonio on 11/13/09.
//  Copyright 2009 Necrosoft. All rights reserved.
//

#import "Tumble.h"


@implementation Tumble

@synthesize boxBody;

- (NSString *)demoName {
	return @"Tumble";
}

/**
 * Initializes the receiver.
 */
- (void)start {
	// create space
	self.space = [PMSpace spaceWithGravity:PMVectMake(0, -1)];
	[space setSubSteps:3];
	[space setActiveHashSize:999 cellDimension:30];
	[space setStaticHashSize:99 cellDimension:200];
	
	PMBody *body;
	PMShape *shape;
	
	// define brick vertices
	NSUInteger brickVertCount = 4;
	PMVect brickVerts[] = {
		PMVectMake(-30,-15),
		PMVectMake(-30, 15),
		PMVectMake( 30, 15),
		PMVectMake( 30,-15),
	};
	
	// define static box vertices.
	PMVect bottomLeft = cpv(-200, -200);
	PMVect topLeft = cpv(-200,  200);
	PMVect topRight = cpv( 200,  200);
	PMVect bottomRight = cpv( 200, -200);
	
	// create box body
	self.boxBody = [PMBody staticBody];
	// add some spin
	[boxBody setAngularVelocity:0.4];
	// don't add the box body to the space to avoid the effects of gravity
	
	// left side
	shape = [PMSegmentShape segmentShapeWithBody:boxBody
										   start:bottomLeft
											 end:topLeft];
	[shape setElasticity:1.0];
	[shape setFriction:1.0];
	[shape setLayers:PMLayerMaskNotGrabbable];
	[space addShape:shape];
	
	// top side
	shape = [PMSegmentShape segmentShapeWithBody:boxBody
										   start:topLeft
											 end:topRight];
	[shape setElasticity:1.0];
	[shape setFriction:1.0];
	[shape setLayers:PMLayerMaskNotGrabbable];
	[space addShape:shape];
	
	// left side
	shape = [PMSegmentShape segmentShapeWithBody:boxBody
										   start:topRight
											 end:bottomRight];
	[shape setElasticity:1.0];
	[shape setFriction:1.0];
	[shape setLayers:PMLayerMaskNotGrabbable];
	[space addShape:shape];
	
	// left side
	shape = [PMSegmentShape segmentShapeWithBody:boxBody
										   start:bottomRight
											 end:bottomLeft];
	[shape setElasticity:1.0];
	[shape setFriction:1.0];
	[shape setLayers:PMLayerMaskNotGrabbable];
	[space addShape:shape];
	
	// add bricks.
	for (int i=0; i<3; i++) {
		for (int j=0; j<7; j++) {
			// body
			PMFloat mass = 1.0;
			PMFloat moment = PMMomentForPolygon(mass, brickVerts, brickVertCount, PMVectZero);
			body = [PMBody bodyWithMass:mass 
								 moment:moment];
			[body setPosition:PMVectMake(i*60 - 150, j*30 - 150)];
			[space addBody:body];
			
			// shape
			shape = [PMPolygonShape polygonShapeWithBody:body 
												vertices:brickVerts
											 vertCount:brickVertCount ];
			[shape setElasticity:0.0];
			[shape setFriction:0.7];
			[space addShape:shape];
		}
	}
}

/**
 * Updates the receiver each frame.
 */
- (void)update:(NSTimeInterval)deltaT {
	// because the box body was not added to the space, we must update its 
	// position manually
	[boxBody updatePositionWithTime:deltaT];
}

/**
 * Returns the demo options to be used for the receiver.
 */
- (PMDemoDrawSettings)drawSettings {
	return PMDemoDrawSettingsDefault;
}

@end
